/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.flowable.CheckAndAbnormal.Service;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.flowable.CheckAndAbnormal.entity.CheckStandArdCycleChildListener;
import com.jeeplus.modules.flowable.CheckAndAbnormal.entity.CheckStandardCycleListener;
import com.jeeplus.modules.flowable.CheckAndAbnormal.mapper.CheckStandArdCycleChildListenerMapper;
import com.jeeplus.modules.flowable.CheckAndAbnormal.mapper.CheckStandardCycleListenerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * 周期点检主附表Service
 *
 * @author 刘明星
 * @version 2020-11-11
 */
@Service
@Transactional(readOnly = true)
public class CheckStandardCycleListenerService extends CrudService<CheckStandardCycleListenerMapper, CheckStandardCycleListener> {

    @Autowired
    CheckStandArdCycleChildListenerMapper checkStandArdCycleChildlistenerMapper;


    public CheckStandardCycleListener get(String id) {
        CheckStandardCycleListener checkStandardCycle = super.get(id);
        checkStandardCycle.setCheckStandArdCycleChildList(checkStandArdCycleChildlistenerMapper.findList(new CheckStandArdCycleChildListener(checkStandardCycle)));
        return checkStandardCycle;
    }

    public List<CheckStandardCycleListener> findList(CheckStandardCycleListener checkStandardCycle) {
        return super.findList(checkStandardCycle);
    }

    public Page<CheckStandardCycleListener> findPage(Page<CheckStandardCycleListener> page, CheckStandardCycleListener checkStandardCycle) {
        return super.findPage(page, checkStandardCycle);
    }

    @Transactional(readOnly = false)
    public void save(CheckStandardCycleListener checkStandardCycle) {
        super.save(checkStandardCycle);
        for (CheckStandArdCycleChildListener checkStandArdCycleChild : checkStandardCycle.getCheckStandArdCycleChildList()) {
            if (checkStandArdCycleChild.getId() == null) {
                continue;
            }
            if (CheckStandArdCycleChildListener.DEL_FLAG_NORMAL.equals(checkStandArdCycleChild.getDelFlag())) {
                if (StringUtils.isBlank(checkStandArdCycleChild.getId())) {
                    checkStandArdCycleChild.setCheckStandard(checkStandardCycle);
                    checkStandArdCycleChild.preInsert();
                    checkStandArdCycleChildlistenerMapper.insert(checkStandArdCycleChild);
                } else {
                    checkStandArdCycleChild.preUpdate();
                    checkStandArdCycleChildlistenerMapper.update(checkStandArdCycleChild);
                }
            } else {
                checkStandArdCycleChildlistenerMapper.delete(checkStandArdCycleChild);
            }
        }
    }

    @Transactional(readOnly = false)
    public void delete(CheckStandardCycleListener checkStandardCycle) {
        super.delete(checkStandardCycle);
        checkStandArdCycleChildlistenerMapper.delete(new CheckStandArdCycleChildListener(checkStandardCycle));
    }

    @Transactional(readOnly = false)
    public void insertAbnormal(String id) {
        CheckStandardCycleListener checkStandardCycle = super.get(id);
        checkStandardCycle.setCheckStandArdCycleChildList(checkStandArdCycleChildlistenerMapper.findList(new CheckStandArdCycleChildListener(checkStandardCycle)));
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        checkStandardCycle.setId(uuid);
        super.mapper.insertAbnormal(checkStandardCycle);
        for (CheckStandArdCycleChildListener checkStandArdCycleChild : checkStandardCycle.getCheckStandArdCycleChildList()) {
            if (CheckStandArdCycleChildListener.DEL_FLAG_NORMAL.equals(checkStandArdCycleChild.getDelFlag())) {
                if(checkStandArdCycleChild.getIsnormal() != null){
                    if (checkStandArdCycleChild.getIsnormal().equals("1")) {
                        checkStandArdCycleChild.setCheckStandard(checkStandardCycle);
                        checkStandArdCycleChild.preInsert();
                        checkStandArdCycleChildlistenerMapper.insert(checkStandArdCycleChild);
                    } else {
                        checkStandArdCycleChildlistenerMapper.delete(checkStandArdCycleChild);
                    }
                }
            }
        }
    }
}